package com.qianfeng.smartdevice.controller;


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.dto.UserDto;
import com.qianfeng.smartdevice.pojo.User;
import com.qianfeng.smartdevice.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("/users")
    public R getAllUsers(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String username, @RequestParam(defaultValue = "-100") int status) {
        PageInfo<User> pageInfo = userService.findAllUserByPage(page, limit, username, status);
        return R.setOk(pageInfo);
    }


    @PostMapping("/user")
    public R addUser(@RequestBody UserDto user) {
        userService.addUser(user);
        return R.setOk(null);
    }

    @DeleteMapping("/users")
    public R deleteByIds(Integer[] ids) {
        int result = userService.deleteByIds(ids);
        return R.setOk(null);

    }

    @PutMapping("/user")
    public R updateUser(@RequestBody UserDto userDto) {
        userService.updateUser(userDto);
        return R.setOk(null);
    }

}
